<?php
namespace app\admin\model;

use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\Model;
use think\model\concern\SoftDelete;

//商品页
class Goods extends Model{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $pk='goods_id';
    //当轮播图字段不为空时,转成数组
    public function getImagesAttr($v)
    {
        if (!empty($v)){
            return explode(',',$v);
        }
    }

    //goods 商品列表的sql语句
     public static function goodsList($where,$order,$data){
         $goods=Goods::
            alias('g')
             ->field('g.goods_id,g.goods_name,g.default_image,g.price,g.recommended,g.recom,g.if_show')
             ->field("FROM_UNIXTIME(g.add_time,'%Y-%m-%d') as add_time")
             ->field('b.brand_name')
             ->field('c.cate_name')
             ->join('brand b','g.brand=b.brand_id')
             ->join('gcategory c','g.cate_id=c.cate_id')
             ->order($order)
             ->where($where)
             ->paginate([
                 'list_rows'=>5,
                 'query'=>$data

             ]);

         return $goods;

         }
     //查询品牌语句
      public static function   goodsPin(){
         $pin=Brand::field('brand_id,brand_name')->select()->toArray();
         return $pin;
          }



     //获取器  下架变图片显示
    public static function   getIfShowImgAttr($v,$data){
        if ($data['if_show']==1){
            return "<img src='/static/images/√.jpg' width='70' height='50'/>";
        }else{
            return "<img src='/static/images/X.jpg'  width='70' height='50' />";
        }
    }
    //获取器  推荐变图片显示
    public static function   getRecommendedImgAttr($v,$data){
        if ($data['recommended']==1){
            return "<img src='/static/images/√.jpg' width='70' height='50'/>";
        }else{
            return "<img src='/static/images/X.jpg'  width='70' height='50' />";
        }
    }
    //获取器  热门变图片显示
    public static function   getRecomImgAttr($v,$data){
        if ($data['recom']==1){
            return "<img src='/static/images/√.jpg' width='70' height='50'/>";
        }else{
            return "<img src='/static/images/X.jpg'  width='70' height='50' />";
        }
    }

            //商品添加
     public static function   getGoodsAdd($post){
         try{
            $res=Goods::strict(true)->insert($post);
         }catch (Exception $e){
              echo $e->getMessage();

              $res=false;
         }

         return $res;

     }

     //商品修改
     public static function   getGoodsEdit($param){
         if (Request::isPost()){
             try{
                 $res=Goods::update($param);
             }catch (Exception $e){
               // $res=$e->getMessage();
                 $res=false;
             }
                return $res;
         }else{
             $find=Goods::find($param['goods_id']);
             return $find;
         }


     }

     //商品删除
     public static function   getGoodsDel($get){
        try{
            $res=Goods::destroy($get['id']);
        }catch (Exception $e){
            $res=false;
        }
            return $res;

     }

     //商品多个删除
     public static function   getGoodsAllDel($get){

        try{
            $get['id']=explode(",",$get['id']);
            $res=Goods::destroy($get['id']);
        }catch (Exception $e){
            $res=false;
        }
            return $res;

         }

   //商品回收站列表
    public static function   GoodsRecycle(){
        try{
            $res=self::onlyTrashed()
                ->alias('g')
                ->field('g.goods_id,g.goods_name,g.default_image,g.price,g.recommended,g.recom,g.if_show')
                ->field("FROM_UNIXTIME(g.add_time,'%Y-%m-%d') as add_time")
                ->field('b.brand_name')
                ->field('c.cate_name')
                ->join('brand b','g.brand=b.brand_id')
                ->join('gcategory c','g.cate_id=c.cate_id')->select();

        }catch (Exception $e){
            $res=false;
        }
        return $res;
    }

    //商品列表页回收站恢复
     public static function   recyclehuifu($get){
         try{
             $res=Goods::onlyTrashed()->find($get['goods_id']);
             $res->restore();
         }catch (\Exception $e){
             $res=false;
             //  echo $e->getMessage();

         }
         return $res;


     }

     //商品页回收站删除
     public static function    recyclehuiDel($get){
         try{
             $res=Goods::destroy($get['id'],true);
         }catch (\Exception $e){
             $res=false;
         }
         return $res;

     }





}